package com.code2roc.fastboot.workflow.controller;

import com.code2roc.fastboot.framework.model.Result;
import com.code2roc.fastboot.framework.util.ConvertOp;
import com.code2roc.fastboot.framework.util.StringUtil;
import com.code2roc.fastboot.model.BaseBootController;
import com.code2roc.miniflow.base.annotation.MiniFlowAutowired;
import com.code2roc.miniflow.base.annotation.MiniFlowComponent;
import com.code2roc.miniflow.desgin.model.MiniFlowProgress;
import com.code2roc.miniflow.desgin.service.MiniFlowProgressService;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@MiniFlowComponent
@RestController
@RequestMapping("/workflow/desgin/progress")
public class WorkFlowProgressController extends BaseBootController {
    @MiniFlowAutowired
    private static MiniFlowProgressService progressService;

    @ResponseBody
    @PostMapping("/list")
    public Object list(@RequestBody Map<String, Object> params) {
        Result result = Result.okResult();
        String sql = "1=1";
        HashMap<String, Object> paramMap = new HashMap<>();
        String progress_name = ConvertOp.convert2String(params.get("progress_name"));
        int pageIndex = ConvertOp.convert2Int(params.get("pageIndex"));
        int pageSize = ConvertOp.convert2Int(params.get("pageSize"));
        if (!StringUtil.isEmpty(progress_name)) {
            sql += " and progress_name like #{progress_name}";
            paramMap.put("progress_name", "%" + progress_name + "%");
        }
        List<MiniFlowProgress> rows = progressService.selectPageList("*", sql, "sort_num,gmt_create", pageIndex, pageSize, paramMap);
        int total = progressService.selectCount(sql, paramMap);
        result.add("rows", rows);
        result.add("total", total);
        return result;
    }

    @ResponseBody
    @PostMapping("/insert")
    @Transactional(rollbackFor = {Exception.class}, isolation = Isolation.READ_UNCOMMITTED)
    public Object insert(@RequestBody MiniFlowProgress entity) {
        Result result = Result.okResult();
        progressService.insert(entity);
        return result;
    }

    @ResponseBody
    @PostMapping("/update")
    @Transactional(rollbackFor = {Exception.class}, isolation = Isolation.READ_UNCOMMITTED)
    public Object update(@RequestBody MiniFlowProgress entity) {
        Result result = Result.okResult();
        progressService.update(entity);
        return result;
    }

    @ResponseBody
    @PostMapping("/detail")
    public Object detail(@RequestBody Map<String, Object> params) {
        Result result = Result.okResult();
        String rowguid = ConvertOp.convert2String(params.get("rowguid"));
        MiniFlowProgress entity = progressService.selectOne(rowguid);
        result.add("obj", entity);
        return result;
    }

    @ResponseBody
    @PostMapping("/delete")
    public Object delete(@RequestBody Map<String, Object> params) {
        Result result = Result.okResult();
        String rowguid = ConvertOp.convert2String(params.get("rowguid"));
        progressService.delete(rowguid);
        return result;
    }
}
